﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ASPItalia.ModelVirtualCasting.Silverlight
{
  public class ButtonCommand<T> : ICommand
  {
    private bool canExecuteValue = false;
    private Action<T> executeAction;
    private Func<T, bool> canExecuteAction;
    public event EventHandler CanExecuteChanged;

    public ButtonCommand(Action<T> executeAction, Func<T, bool> canExecuteAction)
    {
      this.executeAction = executeAction;
      this.canExecuteAction = canExecuteAction;
    }

    public ButtonCommand(Action<T> executeAction)
      : this(executeAction, null)
    {
    }

    public bool CanExecute(object parameter)
    {
      bool canExecute = this.canExecuteAction((T)parameter);

      if (canExecute != canExecuteValue)
      {
        canExecuteValue = canExecute;
        this.CanExecuteChanged(this, EventArgs.Empty);
      }

      return canExecute;
    }

    public void Execute(object parameter)
    {
      this.executeAction((T)parameter);
    }
  }
}
